﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
using DevExpress.XtraPrinting;
using System.Drawing;

namespace QLKS
{
    public partial class frmLapHoaDon : DevExpress.XtraEditors.XtraForm
    {
        private DataTable _table = new DataTable();
        public DTO.PhongDTO _phong = new DTO.PhongDTO();
        public frmLapHoaDon()
        {
            InitializeComponent();
            _table.BeginInit();
            _table.Columns.Add("MaPhieuThue", typeof(int));
            _table.Columns.Add("MaPhong", typeof(int));
            _table.Columns.Add("SoNgayThue", typeof(int));
            _table.Columns.Add("DonGia", typeof(int));
            _table.Columns.Add("ThanhTien", typeof(int));
            _table.EndInit();
        }

        private void frmTaoPhieuThue_Load(object sender, EventArgs e)
        {
            lkePhong.EditValue = _phong.MaPhong;
            lkePhong.Properties.DataSource = BUS.PhongBUS.SelectWithTinhTrang(1).Tables[0];
            lkePhong.Properties.DisplayMember = "TenPhong";
            lkePhong.Properties.ValueMember = "MaPhong";

            lkeName.Properties.DataSource = BUS.KhachHangBUS.SelectAll().Tables[0];
            lkeName.Properties.DisplayMember = "TenKhachHang";
            lkeName.Properties.ValueMember = "TenKhachHang";

            dteNgayTra.EditValue = DateTime.Now;
            gridControl1.DataSource = _table;

            repositoryItemLookUpEdit2.DataSource = BUS.PhongBUS.SelectAll().Tables[0];
            repositoryItemLookUpEdit2.ValueMember = "MaPhong";
            repositoryItemLookUpEdit2.DisplayMember = "TenPhong";
        }
        /// <summary>
        /// Validate dữ liệu nhập và xem là insert hay update (và thực hiện)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!dxValidationProvider1.Validate())
                return;
            var cthoadon = BUS.ChiTietHoaDonBUS.SelectByPhong(Convert.ToInt32(lkePhong.EditValue), DateTime.Parse(dteNgayTra.EditValue.ToString()));
            foreach (DataRow row in _table.Rows)
            {
                if (cthoadon.MaPhong.ToString() == row[0].ToString())
                {
                    MessageBox.Show("Phong này đã có trong chi tiết thanh toán.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            //try
            //{
            _table.Rows.Add(new object[]
                                {
                                    cthoadon.MaPhieuThue,cthoadon.MaPhong,cthoadon.SoNgayThue,cthoadon.DonGia,cthoadon.Tien
                                });
            //}
            //catch (SqlException ex)
            //{
            //    string str = String.Format("Lỗi: {0}", ex.Message);
            //    MessageBox.Show(str, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
        }

        /// <summary>
        /// Thoát và xóa dữ liệu chương trình để giải phóng bộ nhớ
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            //if (_table.Rows.Count > 0)
            //{
            //    DTO.PhieuThuePhongDTO phieuThue = new DTO.PhieuThuePhongDTO() { MaPhong = _phong.MaPhong, NgayThuePhong = DateTime.Parse(dteNgayThue.EditValue.ToString()) };
            //    long id = BUS.PhieuThuePhongBUS.Insert(phieuThue);
            //    DTO.ChiTietPhieuThueDTO ctPhieuThue = new DTO.ChiTietPhieuThueDTO();
            //    foreach (DataRow row in _table.Rows)
            //    {
            //        ctPhieuThue.MaPhieuThue = id;
            //        ctPhieuThue.MaKhachHang = Convert.ToInt32(row["MaKhachHang"]);
            //        BUS.ChiTietPhieuThueBUS.Insert(ctPhieuThue);
            //    }
            //    _phong.TinhTrangPhong = 1;
            //    BUS.PhongBUS.Update(_phong);                
            //}
            _table.Clear();
            GC.Collect();
            Close();
        }
        private void lkeName_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            if (lkeName.EditValue == null || lkeName.Text != lkeName.EditValue.ToString()) txtAddress.EditValue = string.Empty;
            else txtAddress.EditValue = lkeName.GetColumnValue("DiaChi");
        }

        private void gridControl1_ProcessGridKey(object sender, KeyEventArgs e)
        {
                if (gridView1.FocusedRowHandle >= 0)
                {
                    if (gridView1.FocusedRowHandle == 0)
                        gridView1.DeleteRow(gridView1.FocusedRowHandle);
                    gridView1.DeleteSelectedRows();
                }
        }
        //private void printableComponentLink1_CreateReportHeaderArea(object sender, DevExpress.XtraPrinting.CreateAreaEventArgs e)
        //{

        //    DevExpress.XtraPrinting.TextBrick brick;

        //    brick = e.Graph.DrawString("My Report", Color.Navy, new RectangleF(0, 0, 500, 40), DevExpress.XtraPrinting.BorderSide.None);

        //    brick.Font = new Font("Arial", 16);

        //    brick.StringFormat = new DevExpress.XtraPrinting.BrickStringFormat(StringAlignment.Center);

        //}
        //private void Link_CreateMarginalHeaderArea(object sender, CreateAreaEventArgs e)
        //{

        //    PageInfoBrick brick = e.Graph.DrawPageInfo(PageInfo.DateTime, "", Color.DarkBlue,new RectangleF(0, 0, 100, 20), BorderSide.None);
        //    brick.LineAlignment = BrickAlignment.Center;
        //    brick.Alignment = BrickAlignment.Center;
        //    brick.AutoWidth = true;

        //    brick = e.Graph.DrawString("My Report", Color.Navy, new RectangleF(0, 0, 500, 40), DevExpress.XtraPrinting.BorderSide.None);
        //    brick.Font = new Font("Tahoma", 16);
        //    brick.StringFormat = new DevExpress.XtraPrinting.BrickStringFormat(StringAlignment.Center);

        //}
        private void btnThanhToan_Click(object sender, EventArgs e)
        {   
            printableComponentLink1.CreateDocument();
            printableComponentLink1.ShowPreview(); 

            //DTO.HoaDonDTO hd = new DTO.HoaDonDTO(){MaKH=Convert.ToInt32(lkeName.GetColumnValue("MaKhachHang")),NgayLap=DateTime.Parse(dteNgayTra.EditValue.ToString()),TriGia=Convert.ToInt32(colThanhTien.SummaryItem.SummaryValue)};
            //int maHD=BUS.HOADONBUS.Insert(hd);
            //DTO.ChiTietHoaDonDTO cthd = new DTO.ChiTietHoaDonDTO();
            //foreach(DataRow row in _table.Rows)
            //{
            //    cthd.MaPhieuThue = Convert.ToInt32(row["MaPhieuThue"]);
            //    cthd.MaPhong=Convert.ToInt32(row["MaPhong"]);
            //    cthd.MaHoaDon = maHD;
            //    cthd.SoNgayThue = Convert.ToInt32(row["SoNgayThue"]);
            //    cthd.DonGia = Convert.ToInt32(row["DonGia"]);
            //    cthd.Tien = Convert.ToInt32(row["ThanhTien"]);
            //    BUS.ChiTietHoaDonBUS.Insert(cthd);
            //}            
        }

        private void printableComponentLink1_CreateReportHeaderArea(object sender, CreateAreaEventArgs e)
        {
            if (!dxValidationProvider1.Validate())
                return;
            string tenKhach = "Khách Hàng: " + lkeName.Text;
            string diaChi = "Địa Chỉ: " + txtAddress.Text;
            string reportHeader = "Hóa Đơn Thanh Toán\nKhachHang";
            e.Graph.StringFormat = new BrickStringFormat(StringAlignment.Center);
            e.Graph.Font = new Font("Tahoma", 14, FontStyle.Bold);
            RectangleF rec = new RectangleF(0, 0, e.Graph.ClientPageSize.Width, 50);
            e.Graph.DrawString(reportHeader, Color.Black, rec, BorderSide.None);

            e.Graph.Font = new Font("Tahoma", 10, FontStyle.Bold);
            RectangleF recTenKhach = new RectangleF(0, 20, e.Graph.ClientPageSize.Width, 50);
            e.Graph.DrawString(tenKhach, Color.Black, recTenKhach, BorderSide.None);

            RectangleF recDiaChi = new RectangleF(0, 40, e.Graph.ClientPageSize.Width, 50);
            e.Graph.DrawString(diaChi, Color.Black, recDiaChi, BorderSide.None);
        }
    }
}